package router

import (
	"net/http"

	"gitee.com/xchengli/blog-lib/session"

	"github.com/gin-gonic/gin"
)

// authMiddleware 登录验证中间件
func authMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		guest := new(session.GuestSession)
		//检查是否未登录
		if !guest.Init(ctx.Request).CheckIsLogin() {
			ctx.JSON(http.StatusUnauthorized, gin.H{
				"code":     401,
				"message":  "未登录",
				"redirect": "/admin/login",
			})
			ctx.Abort()
			return
		}
		ctx.Next()
	}
}
